package com.example.administrator.zhongyichou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.zhongyichou.activity.MyDiseaseDetailsActivity;
import com.example.administrator.zhongyichou.activity.MyDreamDataActivity;
import com.example.administrator.zhongyichou.help.DateUtils;
import com.example.administrator.zhongyichou.net.protocol.NewsProtocol;
import com.example.administrator.zhongyichou.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lichenyu on 2017/12/4.
 * 描述：
 */

public class NewsAdapter extends ListBaseAdapter {
    public NewsAdapter(Context mContext, List list) {
        super(mContext, list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.news_list_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final NewsProtocol.DataBean bean = (NewsProtocol.DataBean) getData(position);
        holder.txtTitle.setText(bean.getNews_title());
        holder.txtTime.setText(DateUtils.stampToDate(bean.getCreate_time()));
        holder.txtContent.setText(bean.getNews_content());
        holder.txtDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.getSort_id().equals("2")) {//大病
                    Intent intent = new Intent(getContext(), MyDiseaseDetailsActivity.class);
                    intent.putExtra("info_id", bean.getInfo_id());
                    getContext().startActivity(intent);
                } else {
                    Intent intent = new Intent(getContext(), MyDreamDataActivity.class);
                    intent.putExtra("info_id", bean.getInfo_id());
                    getContext().startActivity(intent);
                }
            }
        });
        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_time)
        TextView txtTime;
        @BindView(R.id.txt_content)
        TextView txtContent;
        @BindView(R.id.txt_details)
        TextView txtDetails;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
